jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.environmentChangeSubject = "com.tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChange";
        	plugin.environmentHideComponentsSubject = "tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentHideComponents";
        
        	plugin.currentEnvironmentCacheId = "SPM_CURRENT_ENVIRONMENT_CACHE";
        	
        	plugin.allEnvironmentsId = "AllEnvironmentsId";
        	
        	plugin.prototype.showAllEnvironments;
        	plugin.prototype.screenId;
        	plugin.prototype.nameSuffix;
        	
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.updateComponent = function() {
				this.callGetEnvironments();
			}
            
            plugin.prototype.setUpComponent = function(showAllEnvironments, screenId, nameSuffix, envId) {
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(plugin.environmentChangeSubject, this.handleEnvironmentChange, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleEnvironmentDisplay, this);
                this.showAllEnvironments = showAllEnvironments;
                this.screenId = screenId;
				var envSelect = this.getDescendantOfName("environmentSelect");
				var envLabel = this.getDescendantOfName("environmentLabel");
				this.nameSuffix = "";
				if (nameSuffix) {
					this.nameSuffix = nameSuffix;
					envSelect.setName(envSelect.getName() + nameSuffix);
					envLabel.setName(envLabel.getName() + nameSuffix);
				}
                
                // initialize the environment cache
                var server = tibco.admin.Main.getServer();
                var currentEnvironmentDoc = server.Cache.getDocument(plugin.currentEnvironmentCacheId);
                if(!currentEnvironmentDoc)
                {
    	  			currentEnvironmentDoc = new jsx3.xml.Document();
      				currentEnvironmentDoc.createDocumentElement("Environment","");
      				var environmentElement = currentEnvironmentDoc.createNode(jsx3.xml.Entity.TYPEELEMENT, "EnvironmentInfo", "");
      				if(envId)
      				{ environmentElement.setAttribute("currentEnvironmentId", envId); }
      				else if(showAllEnvironments)
      				{ environmentElement.setAttribute("currentEnvironmentId", plugin.allEnvironmentsId); }
      				else
      				{ environmentElement.setAttribute("currentEnvironmentId", ""); }
      				currentEnvironmentDoc.appendChild(environmentElement);
      				server.Cache.setDocument(plugin.currentEnvironmentCacheId, currentEnvironmentDoc);
      			}
      			else if(envId)
      			{ currentEnvironmentDoc.getFirstChild().setAttribute("currentEnvironmentId", envId); }
                
                this.callGetEnvironments();
            }
            
            plugin.prototype.handleEnvironmentDisplay = function(subject, message, environmentObj) {
            	if(message.screenId == environmentObj.screenId)
            	{
            		var envId = message.environmentId;
            		if(envId)
            		{
            			var currentEnvironmentDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.currentEnvironmentCacheId);
            			currentEnvironmentDoc.getFirstChild().setAttribute("currentEnvironmentId", envId);
            		}
            	
            		environmentObj.updateComponent();
            	}
            }
            
    		//call this method to begin the service call (eg.service.callgetEnvironments();)
    		plugin.prototype.callGetEnvironments = function() {
    		
    			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/dropdown/environment/rules/environmentDropdownRules.xml", "getEnvironments");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL("tibco/admin/monitormanage/uicomponents/dropdown/environment/data/environmentsDropdownData.xml");
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/dropdown/environment/rules/environmentDropdownRules.xml"), "getEnvironments", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/dropdown/environment/data/environmentsDropdownData.xml"));
                }
                
                objService.environmentsObj = this;
                // TODO: use the id to create different caches for each environment component
                //objService.includeAllEnvironments = includeAllEnvironments;
                //objService.environmentComponentId = id;
                
                var envId = plugin.getCurrentEnvironmentId();
                var hideComponents;
   				if(!this.showAllEnvironments && envId == plugin.allEnvironmentsId)
   				{ hideComponents = true; }
   				else
   				{ hideComponents = false; }
				//post a message that whether to hide or display the environment screen components
   				var message = { environmentHideComponents:hideComponents };
   				tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.environmentHideComponentsSubject, message);

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetEnvironmentsSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetEnvironmentsError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetEnvironmentsInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetEnvironmentsSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			var server = tibco.admin.Main.getServer();
      			
      			var environmentsObj = objEvent.target.environmentsObj;
      			
      			if(environmentsObj.showAllEnvironments)
      			{ environmentsObj.createAllEnvironmentsEntry(); }
      			else
      			{ environmentsObj.removeAllEnvironmentsEntry(); }
      			
      			var selectBox = environmentsObj.getDescendantOfName("environmentSelect" + environmentsObj.nameSuffix);
      			// check to see if the current environment cache object exists
   				var envId = plugin.getCurrentEnvironmentId();
   				if(!environmentsObj.showAllEnvironments && envId == plugin.allEnvironmentsId)
   				{ envId = null; }
   				
   				selectBox.setValue(envId);
   				selectBox.repaint();
    		};
    		
    		plugin.prototype.createAllEnvironmentsEntry = function() {
	    		var envDoc = tibco.admin.Main.getServer().Cache.getDocument("SPM_ENVIRONMENT_DROPDOWN_CACHE");
      			var allEnvRecord = envDoc.createNode(jsx3.xml.Entity.TYPEELEMENT, "record", "");
      			allEnvRecord.setAttribute("jsxid", plugin.allEnvironmentsId);
      			allEnvRecord.setAttribute("jsxtext", tibco.admin.Main.getServer().getDynamicProperty("@MM@general@AllEnvironmentsTitle"));
      			envDoc.insertBefore(allEnvRecord, envDoc.getFirstChild());
    		}
    		
    		plugin.prototype.removeAllEnvironmentsEntry = function() {
	    		var envDoc = tibco.admin.Main.getServer().Cache.getDocument("SPM_ENVIRONMENT_DROPDOWN_CACHE");
	    		var records = envDoc.getChildNodes();
	    		for(i = 0; i < records.size(); i++)
	    		{
	    			var record = records.get(i);
	    			if(record.getAttribute("jsxid") == plugin.allEnvironmentsId)
	    			{
	    				envDoc.removeChild(record);
	    				break;
	    			}
	    		}
    		}

    		plugin.prototype.ongetEnvironmentsError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		}

    		plugin.prototype.ongetEnvironmentsInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		}
    		
    		plugin.prototype.environmentChanged = function(envId) {
                var hideComponents;
   				if(!this.showAllEnvironments && envId == plugin.allEnvironmentsId)
   				{ hideComponents = true; }
   				else
   				{ hideComponents = false; }
				//post a message that whether to hide or display the environment screen components
   				var message = { environmentHideComponents:hideComponents };
   				tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.environmentHideComponentsSubject, message);
   				
    			message = { environmentId:envId };
    			tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.environmentChangeSubject, message);
    			
    			// update environment name label
    			var envLabel = this.getDescendantOfName("environmentLabel" + this.nameSuffix);
    			var envDoc = tibco.admin.Main.getServer().Cache.getDocument("SPM_ENVIRONMENT_DROPDOWN_CACHE");
	    		var records = envDoc.getChildNodes();
	    		for(i = 0; i < records.size(); i++)
	    		{
	    			var record = records.get(i);
	    			if(record.getAttribute("jsxid") == envId)
	    			{
	    				envLabel.setText(record.getAttribute("jsxtext"), true);
	    				break;
	    			}
	    		}
    		}
    		
    		plugin.prototype.handleEnvironmentChange = function(subject, message, environmentObj) {
    			var envId = message.environmentId;
    			var envDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.currentEnvironmentCacheId);	
    			envDoc.getFirstChild().setAttribute("currentEnvironmentId", envId);
    		}
    		
    		plugin.getCurrentEnvironmentId = function() {
    			var envDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.currentEnvironmentCacheId);
    			if(envDoc)
    			{ return envDoc.getFirstChild().getAttribute("currentEnvironmentId"); }
    			else
    			{ 
    				// the cache isn't initialized yet so return the all environments id
    				return plugin.allEnvironmentsId; 
    			}
    		}

        });